/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Client.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.SwingConstants;

/**
 *
 * @author Ripper
 */
public class WelcomePanel extends ClientPanel implements ActionListener
{
    private String userName;
    private JLabel nameLabel;
    private JLabel goldLabel;
    
    //view-active-player
    private JLabel activePlayerLabel;
    private DrawPlayerPanel drawPlayerPanel;
    private JButton selectPreviousPlayerButton;
    private JButton selectNextPlayerButton;
    private JButton editPlayerButton;
    
    private String[] playerAttributes = 
    {"Level: ","Physical Damage: ","Magical Damage: ","Physical Armor: ","Magical Armor:","Aggro Multiplier: ",
    "Max Hitpoints: ","Max Mana: ","Max Stamina: ","Hitpoint regen: ","Mana regen: ","Stamina regen: ",
    "Critical Chance: ","Crit Damage Multiplier: ","Attack Speed: ","Movement Speed: ","% Spellvamp: ","% lifesteal: "};
    private JLabel[] playerAttributeLabels;
    
    //private JButton viewEquippedItems;
    private JButton viewInventory;
    private JButton viewAttributePoints;
    
    private JProgressBar experienceBar;
    private JButton createNewPlayerButon;
    //end-view-active-player
    
    private JButton viewLobbiesButton; 
    //TODO
    
    
    public WelcomePanel(ClientWindow parent)
    {
        super(parent);
        
        setLayout(new BorderLayout());
        
        JPanel topNamePanel = new JPanel(new BorderLayout());
        {
            nameLabel = new JLabel("");
            Font f = new Font("Arial", 0, 20);
            nameLabel.setFont(f);
            
            topNamePanel.add(nameLabel, BorderLayout.WEST);
            
            goldLabel = new JLabel("");
            topNamePanel.add(goldLabel, BorderLayout.EAST);
        }
        add(topNamePanel,BorderLayout.NORTH);
        
        JPanel centerPanel = new JPanel(new BorderLayout());
        {
            centerPanel.setBorder(BorderFactory.createLineBorder(Color.BLACK));
            
            JPanel centerTopPanel = new JPanel(new BorderLayout());
            {
                activePlayerLabel = new JLabel("Active player: <no players exist>");
                activePlayerLabel.setHorizontalAlignment(SwingConstants.CENTER);
                centerTopPanel.add(activePlayerLabel, BorderLayout.CENTER);
            }
            centerPanel.add(centerTopPanel, BorderLayout.NORTH);
            JPanel centerCenterPanel = new JPanel(new BorderLayout());
            {
                JPanel ccCenterPanel = new JPanel(new BorderLayout());
                {
                    JPanel cccLeftPanel = new JPanel(new BorderLayout());
                    {
                        selectPreviousPlayerButton = new JButton("  <  ");
                        selectPreviousPlayerButton.addActionListener(this);
                        selectPreviousPlayerButton.setMinimumSize(new Dimension(100,200));
                        selectPreviousPlayerButton.setPreferredSize(new Dimension(100,200));
                        selectPreviousPlayerButton.setMaximumSize(new Dimension(100,200));
                        cccLeftPanel.add(selectPreviousPlayerButton, BorderLayout.CENTER);
                    }
                    ccCenterPanel.add(cccLeftPanel, BorderLayout.WEST);
                    JPanel cccCenterPanel = new JPanel(new BorderLayout());
                    {
                        drawPlayerPanel = new DrawPlayerPanel();
                        drawPlayerPanel.setMinimumSize(new Dimension(200, 400));
                        cccCenterPanel.add(drawPlayerPanel, BorderLayout.CENTER);
                        
                        editPlayerButton = new JButton("Edit Player");
                        editPlayerButton.addActionListener(this);
                        cccCenterPanel.add(editPlayerButton, BorderLayout.SOUTH);
                    }
                    ccCenterPanel.add(cccCenterPanel, BorderLayout.CENTER);
                    JPanel cccRightPanel = new JPanel(new BorderLayout());
                    {
                        selectNextPlayerButton = new JButton("  >  ");
                        selectNextPlayerButton.addActionListener(this);
                        selectNextPlayerButton.setMinimumSize(new Dimension(100,200));
                        selectNextPlayerButton.setPreferredSize(new Dimension(100,200));
                        selectNextPlayerButton.setMaximumSize(new Dimension(100,200));
                        cccRightPanel.add(selectNextPlayerButton, BorderLayout.CENTER);
                    }
                    ccCenterPanel.add(cccRightPanel, BorderLayout.EAST);
                }
                centerCenterPanel.add(ccCenterPanel, BorderLayout.CENTER);
                
                JPanel ccBottomPanel = new JPanel(new BorderLayout());
                {
                    JLabel statsLabel = new JLabel("Statistics: ");
                    ccBottomPanel.add(statsLabel, BorderLayout.NORTH);
                    
                    JPanel statsPanel = new JPanel(new GridLayout(6,6));
                    {
                        playerAttributeLabels = new JLabel[playerAttributes.length];
                        
                        for(int a = 0; a < playerAttributes.length; a++)
                        {
                            JLabel label = new JLabel(playerAttributes[a]);
                            statsPanel.add(label);
                            
                            playerAttributeLabels[a] = new JLabel("");
                            statsPanel.add(playerAttributeLabels[a]);
                        }
                    }
                    ccBottomPanel.add(statsPanel, BorderLayout.CENTER);
                
                    JPanel buttonPanel = new JPanel(new GridLayout(2,1));
                    {
                        viewAttributePoints = new JButton("View Attribute Points");
                        viewAttributePoints.addActionListener(this);
                        buttonPanel.add(viewAttributePoints);

                        //viewEquippedItems = new JButton("View Equipped Items");
                        //viewEquippedItems.addActionListener(this);
                        //buttonPanel.add(viewEquippedItems);

                        viewInventory = new JButton("View Inventory");
                        viewInventory.addActionListener(this);
                        buttonPanel.add(viewInventory);
                    }
                    ccBottomPanel.add(buttonPanel, BorderLayout.EAST);
                }
                centerCenterPanel.add(ccBottomPanel, BorderLayout.SOUTH);
            }
            centerPanel.add(centerCenterPanel, BorderLayout.CENTER);
            JPanel centerBottomPanel = new JPanel(new BorderLayout());
            {
                JPanel bottomStuff = new JPanel(new GridLayout(2,1));
                {
                    experienceBar = new JProgressBar();
                    experienceBar.setMinimum(0);
                    experienceBar.setMaximum(100);
                    experienceBar.setValue(100);
                    experienceBar.setForeground(Color.GREEN);
                    experienceBar.setBackground(Color.ORANGE);
                    bottomStuff.add(experienceBar);
                    
                    createNewPlayerButon = new JButton("Create New Player");
                    createNewPlayerButon.addActionListener(this);
                    bottomStuff.add(createNewPlayerButon);
                }
                centerBottomPanel.add(bottomStuff, BorderLayout.CENTER);
                        
            }
            centerPanel.add(centerBottomPanel, BorderLayout.SOUTH);
        }
        add(centerPanel, BorderLayout.CENTER);
        
        JPanel bottomPanel = new JPanel(new BorderLayout());
        {
            viewLobbiesButton = new JButton(" View Lobbies ");
            viewLobbiesButton.addActionListener(this);
            bottomPanel.add(viewLobbiesButton, BorderLayout.CENTER);
        }
        add(bottomPanel, BorderLayout.SOUTH);
        
        //TODO
    }
    
    public void fillWithData(int gold)
    {
        setGold(gold);
        
        setActivePlayerName("no active player selected. ");
        setExpLevel(0);
        
        String[] data = new String[18];
        for(int a = 0; a < 18; a++)
        {
            data[a] = "";
        }
        setStatValues(data);
    }
    public void fillWithData(int gold, String activePlayerName, String[] statValues, int percentageExpToLevel,
            int id, Color[] colors)
    {
        fillWithData(gold);
        
        setActivePlayerName(activePlayerName);
        
        setStatValues(statValues);
        setExpLevel(percentageExpToLevel);
        
        //TODO: redraw stuff in picturething
        drawPlayerPanel.setData(id, colors, getClientWindow().getClientDomainController());
        drawPlayerPanel.repaint();
    }
    
    public void setExpLevel(int percentage)
    {
        experienceBar.setValue(percentage);
    }
    public void setStatValues(String[] statValues)
    {
        for(int a = 0; a < playerAttributeLabels.length; a++)
        {
            playerAttributeLabels[a].setText(statValues[a]);
        }
    }
    public void setActivePlayerName(String name)
    {
        activePlayerLabel.setText("Active player: "+name);
    }
    public void setGold(int gold)
    {
        goldLabel.setText("Gold: "+gold+"    ");
    }
    public void setUserName(String user)
    {
        userName = user;
        nameLabel.setText("  Welcome, "+userName);
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
        if(e.getSource() == selectNextPlayerButton)
        {
            getClientWindow().selectNextPlayer();
        }
        else if(e.getSource() == selectPreviousPlayerButton)
        {
            getClientWindow().selectPreviousPlayer();
        }
        else if(e.getSource() == editPlayerButton)
        {
            getClientWindow().editPlayer();
        }
        else if(e.getSource() == createNewPlayerButon)
        {
            String newPlayerName = JOptionPane.showInputDialog("Enter player name: ");
            
            getClientWindow().createNewPlayer(newPlayerName);
        }
        else if(e.getSource() == viewAttributePoints)
        {
            getClientWindow().viewAttributePoints();
        }
        /*else if(e.getSource() == viewEquippedItems)
        {
            
        }*/
        else if(e.getSource() == viewInventory)
        {
            getClientWindow().viewInventory();
        }
        else if(e.getSource() == viewLobbiesButton)
        {
            getClientWindow().viewLobbies();
        }
    }
}
